package com.jmu.shoppingcart;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static final Scanner SC = new Scanner(System.in);
    public static Cart cart = new Cart();
    public static Store heixin = new Store();

    static {
        String[] commodityName = {"苹果", "橘子", "橙子", "柠檬", "香蕉", "桃子"};
        double[] commodityPrice = {3.3, 4.4, 1.1, 2.2, 5.5, 6.6};
        int[] commodityNumber = {100, 100, 100, 100, 100, 100};
        for (int i = 0; i < commodityName.length; i++) {
            heixin.add(commodityName[i], commodityPrice[i], commodityNumber[i]);

        }
    }

    public static void main(String[] args) throws IOException {
        menuMain();
    }

    private static void countPrice() {
        System.out.print("您当前的总价为：");
        System.out.println(cart.price().doubleValue());
    }


    private static void updateCart() {
        System.out.print("请输入要修改的商品的名称：");
        String name = SC.nextLine();
        Commodity commodity = cart.findCommodity(name);
        if (commodity != null) {
            System.out.print("请输入您要修改的数量：");
            int number = Integer.valueOf(SC.nextLine());
            int addnumber = cart.cart.get(commodity) - number;
            if (!cart.updateNumber(commodity, number)) {
                System.out.println("您输入的数量有误,请重新输入");
                updateCart();
            } else {
                heixin.commoditys.put(commodity, heixin.commoditys.get(commodity) + addnumber);
            }
        } else {
            System.out.println("购物车内没有该商品,请重新输入");
            updateCart();
        }
    }


    private static void addCommodity() {
        System.out.print("请输入要购买的商品的名称：");
        String name = SC.nextLine();
        Commodity commodity = heixin.findCommodity(name);
        if (commodity == null) {
            System.err.println("商店中没有该商品，请重新输入");
            addCommodity();
        }
        System.out.print("请输入要购买的商品数量：");
        int flag=0;
        while (true) {
            int number = Integer.valueOf(SC.nextLine());
            int repertory = heixin.getRepertory(commodity);
            if(repertory<=0){
                System.out.println("该商品库存不足");
                break;
            }
            if (number > repertory) {
                System.err.print("商品数量输入错误,请重新输入:");
                continue;
            }
            heixin.update(commodity, number);
            cart.add(commodity, number);
            //heixin.show();
            break;
        }
    }

    private static void menuMain() throws IOException {
        while (true) {
            System.out.println("===============欢迎您进入系统=============");
            System.out.println("（1）商店：store");
            System.out.println("（2）购物车：cart");
            System.out.println("（3）退出：exit");
            System.out.println("=========请输入您的命令=============");
            String command = SC.nextLine();
            switch (command) {
                case "store":
                    menuStory();
                    break;
                case "cart":
                    menuCart();
                    break;
                case "exit":
                    System.out.println("退出成功");
                    System.exit(0);
                default:
                    System.err.println("您输入的命令有误请重新输入");
                    menuMain();
            }
        }
    }

    private static void menuStory() {
        while (true) {
            System.out.println("===============欢迎您进入商店============");
            System.out.println("（1）查看商店的全部商品：show");
            System.out.println("（2）添加到购物车：add");
            System.out.println("（2）返回：break");
            System.out.println("（3）退出：exit");
            System.out.println("=========请输入您的命令=============");
            String command = SC.nextLine();
            switch (command) {
                case "show":
                    heixin.showStory();
                    break;
                case "add":
                    addCommodity();
                    break;
                case "break":
                    return;
                case "exit":
                    System.out.println("退出成功");
                    System.exit(0);
                default:
                    System.err.println("您输入的命令有误请重新输入");
                    menuStory();
            }
        }
    }

    private static void menuCart() throws IOException {
        while (true) {
            System.out.println("===============欢迎您进入系统=============");
            System.out.println("（1）查看购物车内商品：show");
            System.out.println("（2）修改购物车内商品数量：update");
            System.out.println("（3）删除商品：delete");
            System.out.println("（4）计算购物车内商品总价钱：count");
            System.out.println("（5）购买：shop");
            System.out.println("（5）返回：break");
            System.out.println("（6）退出：exit");
            System.out.println("=========请输入您的命令=============");
            String command = SC.nextLine();
            switch (command) {
                case "show":
                    cart.showCart();
                    break;
                case "update":
                    updateCart();
                    break;
                case "delete":
                    deleteByName();
                    break;
                case "count":
                    countPrice();
                    break;
                case "shop":
                    // cart.shop();
                    cart.submitOrder();
                    break;
                case "break":
                    return;
                case "exit":
                    System.out.println("退出成功");
                    System.exit(0);
                default:
                    System.err.println("您输入的命令有误请重新输入");
                    menuCart();
            }
        }
    }

    private static void deleteByName() {
        System.out.print("请输入您要删除的商品名称：");
        String name=SC.nextLine();
        Commodity commodity=cart.getCommdityByName(name);
        if(commodity!=null){
            cart.delete(commodity);
            System.out.println("商品删除成功");
        }else{
            System.err.println("商品名称输入错误");
        }

    }
}
